package creational.singleton.volat;

/**
 * 单例的双重检查锁模式
 * volatile 保障多线程正确的处理
 * 内部synchronized 只在首次调用getInstance时生效
 * 双检指的是 2个 判断 null的位置
 */
public class Singleton {
    private volatile static Singleton singleton;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
